$(document).ready(function() {
	loadBuildingTypeList();
});
//填出框
function addBuildingTypeModel(buildingTypeId) {
	
	if (buildingTypeId != null) {		//表示修改
		$("#hidden_building_type_id").val(buildingTypeId);
		$.post("parkingAction!getBuildingType.action",{
			"inParma" : JSON.stringify({
				"buildingTypeId" : buildingTypeId})
		},function(data) {
			data = eval("(" + data + ")");
			if (data.result == "true") {
				var buildingTypes = data.data;
				if (buildingTypes != null){
					var buildingType = buildingTypes[0];
					$("#buildingTypeName").val(buildingType.buildingTypeName);
					$("#buildingArea").val(buildingType.buildingArea);
					$("#comprisingArea").val(buildingType.comprisingArea);
					$("#publicArea").val(buildingType.publicArea);
					$("#costArea").val(buildingType.costArea);
				}
			} else {
				alert("保存失败" + data.msg);
			}
		});
	} else{
		$("#buildingTypeName").val("");
		$("#buildingArea").val("");
		$("#comprisingArea").val("");
		$("#publicArea").val("");
		$("#costArea").val("");
	}
	$("#myModal1").modal("show");
}

//保存修改车位类型
function saveBuildingType () {
	var buildingTypeId = $("#hidden_building_type_id").val();
	
	var community_id = $("input[name='hidden_community_id']").val();
	var buildingTypeName = $("#buildingTypeName").val();
	var buildingArea = $("#buildingArea").val();
	var comprisingArea = $("#comprisingArea").val();
	var publicArea = $("#publicArea").val();
	var costArea = $("#costArea").val();
	
	var obj = {community:{}};
	obj.buildingTypeId=buildingTypeId;
	obj.buildingTypeName = buildingTypeName;
	obj.community.communityId = community_id;
	obj.buildingArea= buildingArea;
	obj.comprisingArea = comprisingArea;
	obj.publicArea = publicArea;
	obj.costArea = costArea;
	var jsarray = new Array();
	var inparam = {data:{}};
	jsarray.push(obj);
	inparam.data=jsarray;
	
	$.post("parkingAction!saveBuildingType.action",{
		"inParma" : JSON.stringify(inparam)
	},function(data) {
		data = eval("(" + data + ")");
		if (data.result == "true") {
			location.href="buildingTypeManager.jsp?communityId="+community_id;
		} else {
			alert("保存失败" + data.msg);
		}
	});
}
//删除车位类型
function removeBuildingType(ptId) {
	$.post("parkingAction!removeBuildingType.action",{
		"inParma" : ptId
	},function(data) {
		data = eval("("+data+")");
	});
}

//查询车位类型列表
function loadBuildingTypeList() {
	var buildingTypeName = $("input[name=keywords]").val();
	var communityId =$("#hidden_community_id").val();
	
	$.post("parkingAction!getBuildingType.action", {
		inParma : JSON.stringify({"buildingTypeName":buildingTypeName,"communityId":communityId})
	}, function(data) {
		data = eval("(" + data + ")");
		$("#demo").myPagination({
			currPage : 1,
			pageSize : 5,
			pageCount : data.pageCount,
			ajax : {
				on : true, // 开启状态
				url : "parkingAction!getBuildingType.action",
				dataType : 'json', // 返回类型
				callback : 'callBack',
				param : {
					"on" : true,
					inParma : JSON.stringify({"buildingTypeName":buildingTypeName,"communityId":communityId})
				},
				ajaxStart : function() {
					return false;
				},
				onClick : function(page) {
					alert(page);
				}
			}
		});
	});
}

function callBack(data) {
	$("#listTable>tbody").children().remove();
	data = eval("(" + data + ")");
	if (data.result != "true") {
		alert("查询失败" + data.msg);
	} else {
		$(data.data)
		.each(
				function(ix, ite) {
					var trStr = '<tr>';
					trStr += '<td class="with-checkbox"><input type="checkbox" name="check" value="'+ite.buildingTypeId+'" /></td>';
					trStr += '<td>'+ite.buildingTypeName+'</td>';
					trStr += '<td>'+StrUtil.strnull(ite.buildingArea)+'</td>';
					trStr += '<td>'+StrUtil.strnull(ite.comprisingArea)+'</td>';
					trStr += '<td>'+StrUtil.strnull(ite.publicArea)+'</td>';
					trStr += '<td>'+StrUtil.strnull(ite.costArea)+'</td>';
					trStr += '<td>';
						trStr += '<a href="javascript:addBuildingTypeModel('+ite.buildingTypeId+');" class="btn">修改</a>';
//						trStr += '<a href="javascript:removeParkType('+ite.buildingTypeId+');" class="btn">删除</a>';
					trStr += '</td>';
					$("#listTable>tbody").append(trStr);
				});
	}
	
}

function removeAllBuildingType() {
	var chk_value =[]; 
	$('input[name="check"]:checked').each(function(){ 
		chk_value.push($(this).val()); 
	}); 
	if (chk_value.length == 0) {
		$.sucmodal("提示", "请勾选要删除的房屋类型",2000);
		return;
	}
	$.altmodal("删除", "您确定要删除所选的房屋类型吗?",1000,function(){
		$.post(
			"parkingAction!removeBuildingType.action",
			{"inParma":JSON.stringify(chk_value)},
			function(data){
				data = eval("("+data+")");
				if(data.result == "true"){
					$.sucmodal("提交成功", "删除成功。",2000,function(){
						location.reload();
	            	});
				}else{
					$.sucmodal("提交失败", "请于管理员联系",2000);
				}
			}
		);
	});
}